Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-240

SMTPAppenderBase: Allow to select charset encoding for email's subject and body

    XMLWordPrintable

Details

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Major Major
    • None
    • None
    • logback-core
    • None

    Description

      Please add String charsetEncoding property to SMTPAppenderBase and use it for email's subject and content encoding.
      Now javaMail default charset encoding is used so only ASCII latin characters can be used in log messages.

      For example my fix for log4j SMTPAppender:
      +
      private String charsetEncoding = "UTF-8";

      public void activateOptions () {
      ..
      addressMessage(msg);
      if (getSubject() != null)

      { ((MimeMessage)msg).setSubject(getSubject(), charsetEncoding); //fix1. Cast looks ugly, but as we always create MimeMessage above, it's ok }

      /*** Send the contents of the cyclic buffer as an e-mail message. */
      @Override
      protected void sendBuffer () {
      /* Note: this code already owns the monitor for this
      appender. This frees us from needing to synchronize on 'cb'. */
      try {
      final MimeBodyPart part = new MimeBodyPart();
      final StringBuilder sb = new StringBuilder();

      String t = layout.getHeader();
      if (t != null)

      { sb.append(t); } //add header

      final int len = cb.length();
      for (int i = 0; i < len; i++) {
      final LoggingEvent event = cb.get();
      sb.append(layout.format(event));
      if (layout.ignoresThrowable()) {
      final String[] s = event.getThrowableStrRep();
      if (s != null) {
      for (int j = 0; j < s.length; j++) { sb.append(s[j]).append(Layout.LINE_SEP); }//for
      }//if
      }//if
      }//for

      t = layout.getFooter();
      if (t != null) { sb.append(t); }

      //fix2:
      t = layout.getContentType();
      if (t.startsWith("text/"))

      { part.setText(sb.toString(), charsetEncoding, t.substring(5)); //remove 'text/' from contentType cause javaMail needs here only subtype (html, plain, etc) }

      else

      { part.setContent(sb.toString(), t); //for non text chartset isn't needed }

      //if

      final Multipart mp = new MimeMultipart();
      mp.addBodyPart(part);
      msg.setContent(mp);

      msg.setSentDate(new Date());
      Transport.send(msg);
      } catch (Exception e)

      { LogLog.error("sendBuffer: Error occured while sending e-mail notification from "+getFrom()+" to "+getTo(), e); }

      //try
      }//sendBuffer

      Attachments

        Activity

          People

            logback-dev Logback dev list
            andreyrybin Andrej Fink
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: